﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using log4net;
using Shpm.App.Commons.Utils;
using Shpm.App.Master.Dao;
using Shpm.App.Master.Bean;
public partial class App_Commons_DishSearchForm : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //判断是否来自订餐画面
            string strFlg=Convert.ToString(Request.QueryString["ORDER_FOODS"]);
            if (!string.IsNullOrEmpty(strFlg))
            {
                ViewState["ORDER_FOODS"] = "TRUE";
            }
            // 取得当前登录的用户id信息
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();

            initPageControls();

            // 接收传递参数为分页编号
            if (Context.Items.Contains("ParamPageIndex"))
            {
                viewSrhRst.PageIndex = Convert.ToInt32(Context.Items["ParamPageIndex"]);
            }
            if (Context.Items.Contains("CMB_TIME"))
            {
                this.ViewState["TIME"] = Convert.ToString(Context.Items["CMB_TIME"]);
                lstDishTime.SelectedValue = Convert.ToString(this.ViewState["TIME"]);
            }
            if (Context.Items.Contains("CMB_CLASS"))
            {
                this.ViewState["CLASS"] = Convert.ToString(Context.Items["CMB_CLASS"]);
                lstDishClass.SelectedValue = Convert.ToString(this.ViewState["CLASS"]);
            }
            if (Context.Items.Contains("CMB_TYPE"))
            {
                this.ViewState["TYPE"] = Convert.ToString(Context.Items["CMB_TYPE"]);
                lstDishType.SelectedValue = Convert.ToString(this.ViewState["TYPE"]);
            }
            if (Context.Items.Contains("CMB_KIND"))
            {
                this.ViewState["KIND"] = Convert.ToString(Context.Items["CMB_KIND"]);
                lstDishKind.SelectedValue = Convert.ToString(this.ViewState["KIND"]);
            }
            if (Context.Items.Contains("CMB_DILIVERY"))
            {
                this.ViewState["DILIVERY"] = Convert.ToString(Context.Items["CMB_DILIVERY"]);
                lstDishDelivery.SelectedValue = Convert.ToString(this.ViewState["DILIVERY"]);
            }


            DishDao dao = new DishDao();
            IList lstRst = null;
            DishBean bean = new DishBean();
            if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["TIME"])))
            {
                bean.DishTime = Convert.ToString(this.ViewState["TIME"]);
            }
            if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["CLASS"])))
            {
                bean.DishClass = Convert.ToString(this.ViewState["CLASS"]);
            }
            if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["KIND"])))
            {
                bean.DishKind = Convert.ToString(this.ViewState["KIND"]);
            }
            if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["TYPE"])))
            {
                bean.DishType = Convert.ToString(this.ViewState["TYPE"]);
            }
            if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["DELIVERY"])))
            {
                bean.DishDelivery = Convert.ToString(this.ViewState["DELIVERY"]);
            }
            if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["NAME"])))
            {
                bean.DishName = Convert.ToString(this.ViewState["NAME"]);
            }

            lstRst = dao.ListDish(bean);
            if (lstRst.Count == 0)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0046").ToString();
            }
            else
            {
                viewSrhRst.DataSource = lstRst;
                viewSrhRst.DataBind();
            }


            string str = Convert.ToString(Request.QueryString["DISHS"]);

            ViewState["DISHS"] = "";
            if (!string.IsNullOrEmpty(str))
            {
                ViewState["DISHS"] = str;
                ArrayList listStr = new ArrayList(str.Split(','));
                RePopulateValues(listStr);

                string[] strId = str.Split(',');
                string newStr = null;


                for (int i = 0; i < strId.Length; i++)
                {
                    DishBean dataBean = new DishBean();
                    DishDao Dao = new DishDao();
                    dataBean.DishId = Convert.ToInt32(strId[i]);
                    dataBean = Dao.GetDish(dataBean);
                    if (string.IsNullOrEmpty(newStr))
                    {
                        newStr = Convert.ToString(Convert.ToString(dataBean.DishId)) + "," + dataBean.DishName;
                    }
                    else
                    {
                        newStr = newStr + "," + Convert.ToString(dataBean.DishId) + "," + dataBean.DishName;
                    }
                }
                ViewState["DISHS"] = newStr;
            }
        }
    }
    // bind common catalog data 
    private void initPageControls()
    {
        DishDao dao = new DishDao();
        CommonDictionaryBean bean = new CommonDictionaryBean();

        // 用餐时间
        bean.Catalog = "M_DISH.DISH_TIME";
        IList lstTime = dao.FindByCatalog(bean);
        lstTime.Insert(0, new CommonDictionaryBean());
        lstDishTime.DataSource = lstTime;
        lstDishTime.DataBind();

        // 荤素类别
        bean.Catalog = "M_DISH.DISH_CLASS";
        IList lstClass = dao.FindByCatalog(bean);
        lstClass.Insert(0, new CommonDictionaryBean());
        lstDishClass.DataSource = lstClass;
        lstDishClass.DataBind();

        // 套菜类别
        bean.Catalog = "M_DISH.DISH_KIND";
        IList lstKind = dao.FindByCatalog(bean);
        lstKind.Insert(0, new CommonDictionaryBean());
        lstDishKind.DataSource = lstKind;
        lstDishKind.DataBind();

        // 菜式类别
        bean.Catalog = "M_DISH.DISH_TYPE";
        IList lstType = dao.FindByCatalog(bean);
        lstType.Insert(0, new CommonDictionaryBean());
        lstDishType.DataSource = lstType;
        lstDishType.DataBind();

        // 送菜类别
        bean.Catalog = "M_DISH.DISH_DELIVERY";
        IList lstDelivery = dao.FindByCatalog(bean);
        lstDelivery.Insert(0, new CommonDictionaryBean());
        lstDishDelivery.DataSource = lstDelivery;
        lstDishDelivery.DataBind();

    }


    // before render
    void Page_PreRender(object sender, EventArgs e)
    {
        // get current doctor responsibility members list
        

        
    }

    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ArrayList  list = RememberOldValues();
        // 设置分页编号
        viewSrhRst.PageIndex = e.NewPageIndex;
    }


    protected void viewSrhRst_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        string dishId = null;
        int index = Convert.ToInt32(e.NewEditIndex);
        // Get the Key value from the GridView control.
        DataKey key = viewSrhRst.DataKeys[index];
        dishId = Convert.ToString(key.Value);
        string url = "~/App/Input/DishUpdForm.aspx";


        // 传递分页编号
        Context.Items["DISH_ID"] = dishId;
        Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;
        ControllerUtils.Transfer(Context, url, true);
    }

    protected void viewSrhRst_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int index = Convert.ToInt32(e.RowIndex);
        // Get the Key value from the GridView control.
        DataKey key = viewSrhRst.DataKeys[index];
        int dishId = Convert.ToInt32(key.Value);

        DishDao dao = new DishDao();
        DishBean bean = new DishBean();
        bean.DishId = dishId;
        dao.DeleteDish(bean);
        ((Design_MasterPage)this.Master).InfoMessage =
            HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0003").ToString();

    }


    // details click
    protected void viewSrhRst_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    }

    // adjust bind data
    protected void viewSrhRst_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void btnSeach_Click(object sender, EventArgs e)
    {
        this.ViewState["TIME"] = lstDishTime.SelectedValue;
        this.ViewState["CLASS"] = lstDishClass.SelectedValue;
        this.ViewState["TYPE"] = lstDishType.SelectedValue;
        this.ViewState["KIND"] = lstDishKind.SelectedValue;
        this.ViewState["DELIVERY"] = lstDishDelivery.SelectedValue;
        this.ViewState["NAME"] = txtDishName.Text;

        //保存值
        ArrayList list = RememberOldValues();

        DishDao dao = new DishDao();
        IList lstRst = null;
        DishBean bean = new DishBean();
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["TIME"])))
        {
            bean.DishTime = Convert.ToString(this.ViewState["TIME"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["CLASS"])))
        {
            bean.DishClass = Convert.ToString(this.ViewState["CLASS"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["KIND"])))
        {
            bean.DishKind = Convert.ToString(this.ViewState["KIND"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["TYPE"])))
        {
            bean.DishType = Convert.ToString(this.ViewState["TYPE"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["DELIVERY"])))
        {
            bean.DishDelivery = Convert.ToString(this.ViewState["DELIVERY"]);
        }
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["NAME"])))
        {
            bean.DishName = Convert.ToString(this.ViewState["NAME"]);
        }

        lstRst = dao.ListDish(bean);
        if (lstRst.Count == 0)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0046").ToString();
        }
        else
        {
            viewSrhRst.DataSource = lstRst;
            viewSrhRst.DataBind();
        }

        ///还原值
        if (!string.IsNullOrEmpty(Convert.ToString(ViewState["DISHS"])))
        {
            RePopulateValues(list);
        }
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        string url = "~/App/Input/PersonJobForm.aspx";

        // 传递分页编号
        Context.Items["CMB_JOB_ID"] = Convert.ToString(this.ViewState["JOB_ID"]);
        Context.Items["CMB_GROUP_ID"] = Convert.ToString(this.ViewState["GROUP_ID"]);
        Context.Items["CMB_STATUS_ID"] = Convert.ToString(this.ViewState["STATUS_ID"]);
        Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;
        ControllerUtils.Transfer(Context, url, true);
    }

    //还原值
    private void RePopulateValues(ArrayList list)
    {
        for (int i = 0; i < this.viewSrhRst.Rows.Count; i++)
        {
            CheckBox chk = this.viewSrhRst.Rows[i].FindControl("CheckBox1") as CheckBox;
            string dishId = this.viewSrhRst.Rows[i].Cells[0].Text;
            chk.Checked = list.IndexOf(dishId) >= 0;
        }
    }

    //保存选中的值
    private ArrayList RememberOldValues()
    {
        ArrayList list = null;
        if (string.IsNullOrEmpty(Convert.ToString(ViewState["DISHS"])))
        {
            list = new ArrayList();
        }
        else
        {
            list = new ArrayList(Convert.ToString(ViewState["DISHS"]).Split(','));
        }

        for (int i = 0; i < this.viewSrhRst.Rows.Count; i++)
        {
            CheckBox chk = this.viewSrhRst.Rows[i].FindControl("CheckBox1") as CheckBox;
            string dishId = this.viewSrhRst.Rows[i].Cells[0].Text;
            string dishName = this.viewSrhRst.Rows[i].Cells[6].Text;



            //如果已经保存过但未选中则删除
            int index = list.IndexOf(dishId);
            if (index >= 0 && !chk.Checked)
            {
                list.RemoveAt(index);
                list.RemoveAt(index);
            }
            //如果没有保存过但未选中则添加
            else if (index < 0 && chk.Checked)
            {
                list.Add(dishId);
                list.Add(dishName);
            }
        }
        //还原字符串
        string dishs = "";
        for (int i = 0; i < list.Count / 2; i++)
        {
            if (string.IsNullOrEmpty(dishs))
            {
                dishs = list[i * 2].ToString() + "," + list[i * 2 + 1].ToString();
            }
            else
            {
                dishs = dishs + "," + list[i * 2].ToString() + "," + list[i * 2 + 1].ToString();
            }
        }
        ViewState["DISHS"] = dishs;
        return list;
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {

        RememberOldValues();
        ArrayList list = null;
        if (string.IsNullOrEmpty(Convert.ToString(ViewState["DISHS"])))
        {
            list = new ArrayList();
        }
        else
        {
            list = new ArrayList(Convert.ToString(ViewState["DISHS"]).Split(','));
        }

        if (ViewState.IsItemDirty("ORDER_FOODS"))
        {
            Response.Write("<script language=JavaScript> window.opener.settext('" + Convert.ToString(ViewState["DISHS"]) + "');</script>");
            //Session.Remove("TXT_ID");
            Response.Write("<script language=JavaScript> window.close();</script>");
        }
        else
        {
            this.hidDishs.Value = Convert.ToString(ViewState["DISHS"]);
            Response.Write("<script language=JavaScript>"
              + "var strDishs='" + this.hidDishs.Value + "'.split(',');"




                                 + "openerSelect = opener.document.getElementById('" + Request.QueryString["CONTROL_ID"] + "');"
                 + "openerSelect.length=0;"
                 + "if(strDishs.length>1){"
                     + "for(var i=0;i<strDishs.length/2;i++)"
                     + "{"
                     + "var newOption = opener.document.createElement('OPTION'); "
                     + "openerSelect.options.add(newOption);"
                     + " newOption.innerText =strDishs[i*2+1];"
                     + "newOption.value = strDishs[i*2];"
                     + "}"

                 + "}"




           + "window.close();</script>");
        }
       
    }
}
